package com.chinacache.maint.workorder.action;

import java.util.ArrayList;
import java.util.List;

import com.chinacache.maint.commons.orm.hibernate.Page;
import com.chinacache.maint.commons.util.DateUtils;
import com.chinacache.maint.commons.util.HttpUtil;
import com.chinacache.maint.commons.web.struts2.CRUDActionSupport;
import com.chinacache.maint.workorder.entity.FlowClass;
import com.chinacache.maint.workorder.entity.WorkOrder;
import com.chinacache.maint.workorder.service.FlowClassManager;
import com.chinacache.maint.workorder.service.WorkOrderManager;

@SuppressWarnings("serial")
public class WorkOrderAction extends CRUDActionSupport<WorkOrder> {
	private WorkOrderManager workOrderManager;
	private FlowClassManager flowClassManager;
	private WorkOrder entity;
	private Integer id;
	private Integer flowClassId;
	public WorkOrder getEntity() {
		return entity;
	}

	private Page<WorkOrder> page = new Page<WorkOrder>();
	private List<FlowClass> flowClasss = new ArrayList<FlowClass>();

	// private String condition;

	@Override
	public String delete() throws Exception {
		if(entity.getWorkOrderSteps().size()>0)
		{
			this.msg="该工单下面已录入子流程，不允许删除！";
			return list();
		}
		workOrderManager.deleteWorkOrder(id);
		return RELOAD;
	}

	@Override
	public WorkOrder getModel() {
		return entity;
	}

	@Override
	public Page<WorkOrder> getPage() {
		return page;
	}

	@Override
	public String list() throws Exception {
		page = workOrderManager.getAllWorkOrder(page);
		return SUCCESS;
	}

	public String input() throws Exception {
		flowClasss = flowClassManager.getAllFlowClass();
		return INPUT;
	}

	@Override
	public void prepare() throws Exception {
		if (id != null) {
			entity = workOrderManager.getWorkOrder(id);
		} else {
			entity = new WorkOrder();
		}
	}

	@Override
	public String save() throws Exception {
		FlowClass flowClass = flowClassManager.getFlowClass(flowClassId);
		String user = (String) HttpUtil.getFromSession("userName");
		entity.setUser(user);
		entity.setTime(DateUtils.getCurrDate());
		entity.setFlowClass(flowClass);
		workOrderManager.saveWorkOrder(entity);
		return RELOAD;
	}

	public void setWorkOrderManager(WorkOrderManager workOrderManager) {
		this.workOrderManager = workOrderManager;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public void setFlowClassManager(FlowClassManager flowClassManager) {
		this.flowClassManager = flowClassManager;
	}

	public List<FlowClass> getFlowClasss() {
		return flowClasss;
	}

	public void setFlowClassId(Integer flowClassId) {
		this.flowClassId = flowClassId;
	}

	// public void setCondition(String condition) {
	// this.condition = condition;
	// }

}
